package com.wanfangdata.demo;

import org.springframework.http.*;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RequestMapping("/demo")
@RestController
public class SearchController {
    //TODO:
    private String appSecret = "应用高级信息APPSecret";
    private String appKey = "应用高级信息APPKey";
    @RequestMapping("/query")
    public String query() {
        String url = "https://api.wanfangdata.com.cn/openwanfang/getQuery";
        String requestBody = "{\n" +
                "  \"collections\": [\"OpenPeriodical\"],\n" +
                "  \"query\": \"(信息 AND 图书馆) AND PublishYear:[1983 TO *]\",\n" +
                "  \"filters\": [{ \"field\": \"SourceDB\", \"value\": \"NSTL\" }],\n" +
                "  \"returned_fields\": [\"Title\", \"Id\", \"PublishYear\"],\n" +
                "  \"sort\": {\n" +
                "    \"sort_name\": \"OfflineScore\"\n" +
                "  }\n" +
                "}";
        return getResponse(url, requestBody);
    }

    @RequestMapping("/get")
    public String get() {
        String url = "https://api.wanfangdata.com.cn/openwanfang/getDoc";
        String requestBody = "{\n" +
                "  \"collection\": \"OpenPeriodical\",\n" +
                "  \"id\": \"dbch202004054\"\n" +
                "}";

        return getResponse(url, requestBody);
    }

    @RequestMapping("/facet")
    public String facet() {
        String url = "https://api.wanfangdata.com.cn/openwanfang/getFacet";
        String requestBody = "{\n" +
                "  \"collections\": [\"OpenPeriodical\"],\n" +
                "  \"query\": \"支持向量机 OR svm\",\n" +
                "  \"filters\": [{ \"field\": \"SourceDB\", \"value\": \"NSTL\" }],\n" +
                "  \"facets\": {\n" +
                "    \"publishyear\": {\n" +
                "      \"field\": \"PublishYear\",\n" +
                "      \"sort\": {\n" +
                "        \"by\": \"index\",\n" +
                "        \"order\": \"ASC\"\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}";
        return getResponse(url, requestBody);
    }

    private String getResponse(String url, String requestBody){
        String md5 = DigestUtils.md5DigestAsHex((appSecret).getBytes()).toLowerCase();

        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders header = new HttpHeaders();

        header.add("X-Ca-AppKey", appKey);
        header.add("X-Ca-Signature", md5);
        header.setContentType(MediaType.APPLICATION_JSON);

        // 发送POST请求
        HttpEntity<String> request = new HttpEntity<>(requestBody, header);
        ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);

        if (response.getStatusCode() != HttpStatus.OK) {
            return String.format("检索出错, 状态码:%d, 错误:%s", response.getStatusCode(), response.getBody());
        }

        return response.getBody();
    }
}
